package com.example.xyxwebbackend001.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.xyxwebbackend001.model.DTO.UserDTO;
import com.example.xyxwebbackend001.model.entity.User;
import com.example.xyxwebbackend001.model.vo.PartUserVO;
import com.example.xyxwebbackend001.model.vo.SecurityInfoVO;
import com.example.xyxwebbackend001.model.vo.UserInfoVO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface USERMapper extends BaseMapper<User> {
    @Select("SELECT user_id, username, email, avatar_path FROM user WHERE username = #{username} AND password = #{password}")
    UserDTO authUserByNameAndPassword(String username, String password);

    @Select("SELECT username FROM user WHERE username = #{username}")
    String getUsername(String username);

    @Insert("INSERT INTO user (username, password, email) VALUES (#{username}, #{password}, #{email})")
    @Options(useGeneratedKeys = true, keyProperty = "userId") // 自增主键
    int insertUser(User user);

    @Select("SELECT COUNT(*) FROM user WHERE email = #{email}")
    int selectCountByEmail(String email);

    @Select({
            "<script>",
            "SELECT u.user_id, u.username, u.email, u.status " +
                    "FROM user u " +
                    "JOIN role_user ru ON u.user_id = ru.user_id " +
                    "JOIN roles r ON ru.role_id = r.role_id " +
                    "WHERE r.role_code = #{roleCode} " +
                    "<if test='keyword != null and keyword != \"\"'> " +
                    "AND (u.username LIKE CONCAT('%', #{keyword}, '%') OR u.email LIKE CONCAT('%', #{keyword}, '%')) " +
                    "</if>",
            "</script>"
    })
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "username", column = "username"),
            @Result(property = "email", column = "email"),
            @Result(property = "status", column = "status")
    })
    List<UserInfoVO> selectUsersByRole(@Param("roleCode") String roleCode,
                                       @Param("keyword") String keyword);

    @Update("UPDATE user SET password = #{newPassword} WHERE user_id = #{userId}")
    int resetPassword(@Param("userId") Integer userId, @Param("newPassword") String newPassword);

    @Update("UPDATE user SET status = #{status} WHERE user_id = #{userId}")
    int updateUserStatus(@Param("userId") Integer userId, @Param("status") Integer status);

    @Select("SELECT * FROM user WHERE user_id = #{userId}")
    User getUserById(Integer userId);

    @Select("SELECT avatar_path AS avatarPath FROM user WHERE user_id = #{userId}")
    String getUserAvatarPath(@Param("userId") Integer userId);

    @Update("UPDATE user SET avatar_path = #{avatarPath} WHERE user_id = #{userId}")
    int updateUserAvatar(@Param("userId") Integer userId, @Param("avatarPath") String avatarPath);

    @Select("SELECT user_id as userId, username, avatar_path FROM user WHERE user_id = #{userId}")
    PartUserVO getPartUserById(@Param("userId") Integer userId);

    @Update("UPDATE user SET password = #{newPassword} WHERE email = #{email}")
    int forgetPassword(@Param("email") String email, @Param("newPassword") String newPassword);

    @Select("SELECT password, email FROM user WHERE user_id = #{userId}")
    SecurityInfoVO getSecurityInfo(@Param("userId") Integer userId);

    @Update("UPDATE user SET email = #{email} WHERE user_id = #{userId}")
    int updateEmail(@Param("userId") Integer userId, @Param("email") String email);
}
